package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加菜品
     * @param dishDTO
     */
    @Transactional //注解事务控制
    public void addNewDish(DishDTO dishDTO) {
        //向菜品表添加一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.DISABLE);
        log.info(dish.toString());
        dishMapper.addNewDish(dish);
        Long dishId = dish.getId();
        //向口味表添加多条数据
        List<DishFlavor> list =dishDTO.getFlavors();
        if (list!=null && list.size()>0){
            for (DishFlavor dishFlavor: list ){
                dishFlavor.setDishId(dishId);
            }
            log.info("口味{}",list);
            dishFlavorMapper.addFlavor(list);
        }


    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult getAllDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = (Page<DishVO>) dishMapper.getAllDish(dishPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;

    }

    /**
     * 设置菜品状态
     * @param id
     * @param status
     */
    public void setDishStatus(Long id, Integer status) {
        Dish dish = Dish.builder()
                        .id(id)
                        .status(status)
                        .build();
        dishMapper.updateDish(dish);
    }

    /**
     * 查询菜品 by id
     * @param id
     * @return
     */
    @Override
    public DishVO getDishById(Long id) {

        DishVO dishVO = dishMapper.getDishById(id);
        dishVO.setFlavors(dishFlavorMapper.getDishById(id));
        return  dishVO;
    }

    /**
     * 更新菜品
     * @param dishDTO
     */
    @Transactional
    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        List<DishFlavor> list = dishDTO.getFlavors();
        log.info("信息{}",list.toString());
        dishMapper.updateDish(dish);
        dishFlavorMapper.deleteDishFlavor(dish.getId());
        if (list!= null && list.size()>0){
            for (DishFlavor d :list) {
                d.setDishId(dish.getId());
            }
            dishFlavorMapper.addFlavor(list);
        }
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    public void deleteDishs(List<Integer> ids) {
        dishMapper.deleteDishs(ids);
    }

    @Override
    public List<Dish> getDishsByType(Long categoryId) {
        return dishMapper.getDishsByType(categoryId);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getDishById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
